#include "GY_Timer.h"
#include <QDebug>

GY_Timer::GY_Timer(const QString &guid, QGraphicsItem *parent)
    :GY_BasicsCalculators(guid,this,parent)
{
    //图元类型
    GY_Object::setUserData(GY_ENUM::ClassType,QVariant("Timer"));
    initializationPropertyInfo();
    m_pTimer = new QTimer(this);doStart();
    connect(m_pTimer, &QTimer::timeout, [=]{
        m_PropertyInfoMap["Time"].propertyValue = m_PropertyInfoMap["Time"].propertyValue.toInt() + m_PropertyInfoMap["Period"].propertyValue.toInt();
//        qDebug()<<m_PropertyInfoMap["Time"].propertyValue.toInt();
    });
}

GY_Timer::~GY_Timer()
{

}

void GY_Timer::initializationPropertyInfo()
{
    GY_PropertyInfo ObjectName(this);
    ObjectName.propertyName = "ObjectName";
    ObjectName.propertyValue = QString();
    ObjectName.updateType = Update;
    ObjectName.propertyType = QStringPropertyType;
    ObjectName.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_Timer::updateObjectName;
    m_PropertyInfoMap.insert(ObjectName.propertyName,ObjectName);
    m_PropertyInfoMap[ObjectName.propertyName].setConnect();

    GY_PropertyInfo Timer(this);
    Timer.propertyName = "Time";
    Timer.propertyValue = 0;
    Timer.updateType = Update;
    Timer.propertyType = IntPropertyType;
    Timer.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_Timer::updateTimer;
    m_PropertyInfoMap.insert(Timer.propertyName,Timer);
    m_PropertyInfoMap[Timer.propertyName].setConnect();

    GY_PropertyInfo Period(this);
    Period.propertyName = "Period";
    Period.propertyValue = 1;
    Period.updateType = Update;
    Period.propertyType = IntPropertyType;
    Period.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_Timer::updatePeriod;
    m_PropertyInfoMap.insert(Period.propertyName,Period);
    m_PropertyInfoMap[Period.propertyName].setConnect();

    GY_PropertyInfo Repeat(this);
    Repeat.propertyName = "Repeat";
    Repeat.propertyValue = true;
    Repeat.updateType = Update;
    Repeat.propertyType = BoolPropertyType;
    Repeat.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_Timer::updateRepeat;
    m_PropertyInfoMap.insert(Repeat.propertyName,Repeat);
    m_PropertyInfoMap[Repeat.propertyName].setConnect();

    GY_PropertyInfo Synchronize(this);
    Synchronize.propertyName = "Synchronize";
    Synchronize.propertyValue = false;
    Synchronize.updateType = Update;
    Synchronize.propertyType = BoolPropertyType;
    Synchronize.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_Timer::updateSynchronize;
    m_PropertyInfoMap.insert(Synchronize.propertyName,Synchronize);
    m_PropertyInfoMap[Synchronize.propertyName].setConnect();

    GY_PropertyInfo Enabled(this);
    Enabled.propertyName = "Enabled";
    Enabled.propertyValue = true;
    Enabled.updateType = Update;
    Enabled.propertyType = BoolPropertyType;
    Enabled.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_Timer::updateEnabled;
    m_PropertyInfoMap.insert(Enabled.propertyName,Enabled);
    m_PropertyInfoMap[Enabled.propertyName].setConnect();

    GY_PropertyInfo EnsureEveryPeriodExecutes(this);
    EnsureEveryPeriodExecutes.propertyName = "EnsureEveryPeriodExecutes";
    EnsureEveryPeriodExecutes.propertyValue = false;
    EnsureEveryPeriodExecutes.updateType = Update;
    EnsureEveryPeriodExecutes.propertyType = BoolPropertyType;
    EnsureEveryPeriodExecutes.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_Timer::updateEnsureEveryPeriodExecutes;
    m_PropertyInfoMap.insert(EnsureEveryPeriodExecutes.propertyName,EnsureEveryPeriodExecutes);
    m_PropertyInfoMap[EnsureEveryPeriodExecutes.propertyName].setConnect();
}

bool GY_Timer::updateTimer(const QString &, const QVariant &)
{
    return false;
}

bool GY_Timer::updatePeriod(const QString &name, const QVariant &value)
{
    if(!value.canConvert<int>()) {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue = value;
    return true;
}

bool GY_Timer::updateRepeat(const QString &name, const QVariant &value)
{
    if(!value.canConvert<bool>()) {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue = value;
    return true;
}

bool GY_Timer::updateSynchronize(const QString &name, const QVariant &value)
{
    if(!value.canConvert<bool>()) {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue = value;
    return true;
}

bool GY_Timer::updateEnabled(const QString &name, const QVariant &value)
{
    if(!value.canConvert<bool>()) {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue = value;
    return true;
}

bool GY_Timer::updateEnsureEveryPeriodExecutes(const QString &name, const QVariant &value)
{
    if(!value.canConvert<bool>()) {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue = value;
    return true;
}

void GY_Timer::doStart()
{
    m_pTimer->start(m_PropertyInfoMap["Period"].propertyValue.toInt());
    m_PropertyInfoMap["Enabled"].propertyValue = true;
}

void GY_Timer::doStop()
{
    m_pTimer->stop();
    m_PropertyInfoMap["Enabled"].propertyValue = false;
}

void GY_Timer::doReset()
{
    m_PropertyInfoMap["Time"].propertyValue = 0;
}

void GY_Timer::evElapsed()
{

}



